package org.example.springweibo.controller;

import org.apache.ibatis.annotations.Param;
import org.example.springweibo.jsonresult.JsonResult;
import org.example.springweibo.pojo.dto.UserDTO;
import org.example.springweibo.pojo.dto.WeiboDTO;
import org.example.springweibo.pojo.entity.Weibo;
import org.example.springweibo.service.WeiboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/v1/weibo")
public class WeiboController {

    @Autowired
    private WeiboService weiboService;

    @GetMapping("/weibo-list")
    public JsonResult weiboList(){
        return JsonResult.ok(weiboService.getWeiboList());
    }

    @PostMapping("/add-weibo")
    public JsonResult addWeibo(WeiboDTO weibo){
        return JsonResult.ok(weiboService.addWeibo(weiboResult(weibo)));
    }

    @GetMapping("/weibo-list-content")
    public JsonResult weiboListByContent(WeiboDTO weibo){
        return JsonResult.ok(weiboService.getWeiboListByContent(weiboResult(weibo)));
    }

    @PostMapping("/weibos")
    public JsonResult weibos(UserDTO user){
        return JsonResult.ok(weiboService.getWeibos(user.getId()));
    }

    @PostMapping("/update-weibo")
    public JsonResult updateWeibo(WeiboDTO weibo){
        return JsonResult.ok(weiboService.updateWeibo(weiboResult(weibo)));
    }

    @DeleteMapping("/delete-weibo")
    public JsonResult deleteWeibo(WeiboDTO weibo){
        return JsonResult.ok(weiboService.deleteWeibo(weiboResult(weibo).getId()));
    }

    @DeleteMapping("/comment-delete/{id}")
    public JsonResult deleteComment(@PathVariable("id")int commentId){
        return JsonResult.ok(weiboService.deleteComment(commentId));
    }

    private Weibo weiboResult(WeiboDTO weiboDTO){
        Weibo weibo = new Weibo();
        weibo.setId(weiboDTO.getId());
        weibo.setContent(weiboDTO.getContent());
        weibo.setUserId(weiboDTO.getUserId());
        return weibo;
    }
}
